import _ from 'lodash';
import { Navigate, useLocation } from 'umi';
import BasicLayout from './basicLayout';
import MainLayout from './mainLayout';

export default function () {
  const location = useLocation();
  const pathname = location.pathname;

  if (_.startsWith(pathname, '/login')) {
    return <BasicLayout />;
  }

  // 判断 token 是否存在
  const isLogin = window.sessionStorage.getItem('Token');
  if (isLogin) {
    // 登录以后根路径则跳转至主页
    if (_.startsWith(pathname, '/main')) {
      return <MainLayout />;
    }
  }
  return <Navigate to={'/login'} />;
}
